unit filereader;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

type
    Treadstruct = record
      values:array [0..7] of double;
      strings:array [0..7] of string;
    end;

    TFileReader = class
    private { Private declarations }
      var tagsfile:TextFile;


    public  { Public declarations }
      HasReadFile:Boolean;
      constructor Create;
      Destructor  Destroy; override;

      //Handlers:
      function open_network_file(indfile:string):boolean;
      procedure close_network_file;
      function remove_comment(indline:string):string;
      function remove_whitespace(indline:string):string;
      function parseline(textline:string):Treadstruct;
//      function parseline(inputline:string; var name:string; var id:integer):TLinetype;
//      function WriteTagsUnit(tagsunitfilename:string):boolean;
//      function ReadTagsFile(tagsfilename:string):boolean;
//      procedure test_unit;
  end;
(*
AssignFile(tagsfile,tagsunitfilename);
Rewrite(tagsfile);
CloseFile(tagsfile);


AssignFile(tagsfile,tagsunitfilename);
reset(tagsfile);
while not(EOF(tagsfile)) do
begin
  readln(tagsfile,indstr);
end;
CloseFile(tagsfile);

*)
implementation

constructor TFileReader.Create;
begin
  Inherited;
  self.HasReadFile := false;
end;

destructor TFileReader.Destroy;
begin
  close_network_file;
  Inherited;
end;

function TFileReader.open_network_file(indfile:string):boolean;
var iores:integer;
begin
  AssignFile(tagsfile,indfile);
  {$I-}
  reset(tagsfile);
  {$I+}
  iores:=IOResult;
  if iores<>0 then
  begin
    Writeln('Error(',iores,')  opening file: "',indfile,'"');
    result:=false;
    readln;
  end
  else
  begin
    result:=true;
  end;
end;

procedure TFileReader.close_network_file;
begin
  if self.HasReadFile then
  begin
    CloseFile(self.tagsfile);
  end;
end;

function TFileReader.remove_whitespace(indline:string):string;
var j:integer;
    result_str:string;
begin
  result_str:='';
  for j:=1 to Length(indline) do
  begin
    if (indline[j]<>' ') AND (indline[j]<>#9) then
    begin
      result_str := result_str + indline[j];
    end;
  end;
  remove_whitespace := result_str;
end;

function TFileReader.remove_comment(indline:string):string;
  var j:integer;
      result_str:string;
begin
  result_str:='';
  j:=1;
  while (j<=Length(indline)) AND (indline[j]<>';') do
  begin
    result_str := result_str + indline[j];
    j:=j+1;
  end;
  remove_comment := result_str;
end;

function TFileReader.parseline(textline:string):Treadstruct;

//function ParseLine(ExpectedType:TLineType):TFileLineParsed;
  var retur:Treadstruct;

  function FindEqualSign:integer;
    var i:integer;
  begin
    i:=1;
    while (i<length(textline)) and (textline[i]<>'=') do
    begin
      i:=i+1;
    end;
    result:=i;
  end;

  function RemoveQotationMarks(indstr:string; var errorflag:boolean):string;
    var i:integer;
  begin
    errorflag:=true;
    if (indstr[1]='"') and (indstr[length(indstr)]='"') then
    begin
      indstr:=Copy(indstr,2,length(indstr)-2);
      write('<String found (',indstr,')>');

      errorflag:=false;
    end
    else
    begin
      write('<String not found!>');

    end;
    result:=indstr;
  end;

  function GetTag:String;
  var i:integer;
      EqualPos:integer;
      retur:string;
  begin
    i:=1;
    retur:='';
    while (i<=length(textline)) and (textline[i]<>'=') do
    begin
      retur:=retur+textline[i];
      i:=i+1;
    end;
    result:=retur;
  end;

  function GetValue:string;
    var i:integer;
        EqualPos:integer;
        retur:string;
  begin
    EqualPos:=FindEqualSign;
    i:=EqualPos;
    retur:='';
    while (i<=length(textline))do
    begin
      retur:=retur+textline[i];
      i:=i+1;
    end;
    result:=retur;
  end;

var value:double;
    errorflag:boolean;
    line_error_code:integer;
    code:integer;

begin
  (*
  case ExpectedType of
    rec: //record start
    Begin
      retur.identifier:='Record';
      retur.value_str:=GetValue;
      retur.LineType:=ExpectedType;
      write('<Record>');

    end;
    lst: //list start
    begin
      retur.identifier:='List';
      retur.value_str:=GetValue;
      retur.LineType:=ExpectedType;
      write('<List>');
    end;
    tag: //Tag end
    begin
      retur.identifier:=GetTag;
      retur.value_str:=GetValue;
      retur.value_str:=Copy(retur.value_str,2,length(retur.value_str)-1);
      if length(retur.value_str)>=1 then
      begin
        Val(retur.value_str ,retur.value,code);
      end
      else
      begin
        retur.value:=0;
        code:=-1;
      end;
      case code of
        1: //If it is a non numeric from Val() above
        begin
          retur.value_str:=RemoveQotationMarks(retur.value_str,errorflag);
          if errorflag=true then write ('<malformed string>');
        end;
        2:
        begin
          write('(Incorrect number format)');
        end;
      end;
      writeln('Tagvalue "',retur.value_str,'":=',retur.value:3:2,' code=',code);
      retur.LineType:=ExpectedType;
    end;

    lnd: //List end
    begin
      retur.identifier:='EndList';
      retur.value_str:='EndList';
      write('<EndList>');
      retur.LineType:=ExpectedType;
    end;

    rnd: //Record end
    begin
      retur.identifier:='EndRecord';
      retur.value_str:='EndRecord';
      write('<EndRecord>');
      retur.LineType:=ExpectedType;
    end;
    else
    begin
      retur.identifier:='Unknown';
      retur.value_str:='Unknown';
      retur.value:=-1;
      write('<???>');
      retur.LineType:=unk;
    end;
  end;
  *)

end;

(*
begin
  firstchar:=textline[1];
  case firstchar of
  '(':
  Begin
    ident:=GetIdent(ExpectNonTagLine);
    case ident of
      'REC':
      begin
        ParsedStructure:=ParseLine(rec);
      end;
      'LIS':
      begin
        ParsedStructure:=ParseLine(lst);
      end;
      else
      begin
        write('Unknown:"',ident,'"');
      end;
    end;
  end;
  ')':
  Begin
    ident:=GetIdent(ExpectNonTagLine);
    write('"',ident,'"');
    case ident of
      'REC':
      begin
        ParsedStructure:=ParseLine(rec);
      end;
      'LIS':
      begin
        ParsedStructure:=ParseLine(lst);
      end;
      else
      begin
        write('Unknown:"',ident,'"');
      end;
    end;
  end;
  else
  Begin
    ident:=GetIdent(ExpectTagLine);
    ParsedStructure:=ParseLine(tag);
    write('"',ident,'"');
    writeln('Tag');
  end;
end;
*)

end.

